package com.example.d1216;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import org.litepal.LitePal;

import java.util.List;

public class LieBiaoActivity extends AppCompatActivity implements View.OnClickListener {

    private ListView list;
    private Button liebiaoM;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lie_biao);
        initView();
    }

    private void initView() {
        list = (ListView) findViewById(R.id.list);
        list.setAdapter(new myAdapter());
        liebiaoM = (Button) findViewById(R.id.liebiaoM);

        liebiaoM.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.liebiaoM:
                startActivity(new Intent(this, MainActivity.class));
                break;
        }
    }

    private class myAdapter extends BaseAdapter {
        List<User> list = LitePal.findAll(User.class);

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int i) {
            return null;
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            ViewHolder viewHolder;
            if(view==null) {
                view = LayoutInflater.from(LieBiaoActivity.this).inflate(R.layout.layout, viewGroup, false);
                viewHolder= new ViewHolder(view);
                view.setTag(viewHolder);
            }else {
                viewHolder= (ViewHolder) view.getTag();
            }
            viewHolder.biaohaoE.setText("编号："+list.get(i).getKahao());
            viewHolder.yueerE.setText("余额："+list.get(i).getKayuer() );
            return view;
        }

        public class ViewHolder {
            public View rootView;
            public TextView biaohaoE;
            public TextView yueerE;

            public ViewHolder(View rootView) {
                this.rootView = rootView;
                this.biaohaoE = (TextView) rootView.findViewById(R.id.biaohaoE);
                this.yueerE = (TextView) rootView.findViewById(R.id.yueerE);
            }

        }
    }
}
